package com.ihrm.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ihrm.common.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ihrm.modules.sys.entity.PermissionEntity;
import com.ihrm.modules.sys.service.PermissionService;
import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.R;


/**
 * 
 * @author gaogzhen
 * @email gaogzhen@foxmail.com
 * @date 2020-12-17 20:56:33
 */
@RestController
@RequestMapping("sys/permission")
@Api(tags = "Permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = permissionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public R info(@PathVariable("id") Long id) {
        PermissionEntity permission = permissionService.getById(id);

        return R.ok().put("permission", permission);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("添加")
    public R save(@RequestBody PermissionEntity permission) {
        permissionService.save(permission);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public R update(@RequestBody PermissionEntity permission) {
        // ValidatorUtils.validateEntity(permission);
        permissionService.updateById(permission);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation("删除")
    public R delete(@RequestBody Long[] ids) {
        permissionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/levelPerms")
    @ApiOperation("查询所有权限(层级)")
    public R levelPerms(@RequestParam Map<String, Object> params) {
        List<PermissionEntity> list = permissionService.levelPermsQueue(params);
        return R.ok().put("levelPerms", list);
    }
    @GetMapping("/levelPermsStack")
    @ApiOperation("查询所有权限(层级)")
    public R levelPermsStack(@RequestParam Map<String, Object> params) {
        List<PermissionEntity> list = permissionService.levelPermsStack(params);
        return R.ok().put("levelPerms", list);
    }

    @GetMapping("/queryByRoleId/{id}")
    @ApiOperation("根据角色id查询权限")
    public R queryByRoleId(@PathVariable String id) {
        List<String> permIds = permissionService.queryByRoleId(id);
        return R.ok().put("permIds", permIds);
    }
}
